package com.zenithmind.document.pojo.domain;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.zenithmind.common.base.BaseEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDateTime;

/**
 * 文档访问记录实体类
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("document_access_log")
@Schema(description = "文档访问记录实体")
public class DocumentAccessLog extends BaseEntity {

    /**
     * 文档ID
     */
    @TableField("document_id")
    @Schema(description = "文档ID")
    private String documentId;

    /**
     * 用户ID
     */
    @TableField("user_id")
    @Schema(description = "用户ID")
    private String userId;

    /**
     * 访问类型（view, download, preview, edit）
     */
    @TableField("access_type")
    @Schema(description = "访问类型")
    private String accessType;

    /**
     * 设备类型
     */
    @TableField("device_type")
    @Schema(description = "设备类型")
    private String deviceType;

    /**
     * IP地址
     */
    @TableField("ip_address")
    @Schema(description = "IP地址")
    private String ipAddress;

    /**
     * 用户代理
     */
    @TableField("user_agent")
    @Schema(description = "用户代理")
    private String userAgent;

    /**
     * 访问时长（秒）
     */
    @TableField("duration")
    @Schema(description = "访问时长（秒）")
    private Integer duration;

    /**
     * 访问时间
     */
    @TableField("access_time")
    @Schema(description = "访问时间")
    private LocalDateTime accessTime;

    // ========== 静态工厂方法 ==========

    /**
     * 创建查看日志
     */
    public static DocumentAccessLog createViewLog(String documentId, String userId, String userAgent, String ipAddress) {
        DocumentAccessLog log = new DocumentAccessLog();
        log.setDocumentId(documentId);
        log.setUserId(userId);
        log.setAccessType("view");
        log.setUserAgent(userAgent);
        log.setIpAddress(ipAddress);
        log.setAccessTime(LocalDateTime.now());
        return log;
    }

    /**
     * 创建下载日志
     */
    public static DocumentAccessLog createDownloadLog(String documentId, String userId, String userAgent, String ipAddress) {
        DocumentAccessLog log = new DocumentAccessLog();
        log.setDocumentId(documentId);
        log.setUserId(userId);
        log.setAccessType("download");
        log.setUserAgent(userAgent);
        log.setIpAddress(ipAddress);
        log.setAccessTime(LocalDateTime.now());
        return log;
    }

    /**
     * 创建分享日志
     */
    public static DocumentAccessLog createShareLog(String documentId, String userId, String shareType, String userAgent, String ipAddress) {
        DocumentAccessLog log = new DocumentAccessLog();
        log.setDocumentId(documentId);
        log.setUserId(userId);
        log.setAccessType("share");
        log.setUserAgent(userAgent);
        log.setIpAddress(ipAddress);
        log.setAccessTime(LocalDateTime.now());
        // shareType 可以存储在额外字段中，这里暂时忽略
        return log;
    }
}
